-- Copyright (c) 2025 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

local class = require 'mc.class'
local singleton = require 'mc.singleton'
local service = require 'spdm.service'

local trusted_component_mdb = class()

function trusted_component_mdb:ctor(responder_collection)
    self.m_responder_collection = responder_collection
    self.m_mdb_components = {}
end

function trusted_component_mdb:init()
    self:regist_signals()
end

function trusted_component_mdb:regist_signals()
    self.m_responder_collection.m_component_added:on(function(...)
        self:new_component_to_mdb(...)
    end)
end

function trusted_component_mdb:new_component_to_mdb(component_info)
    local obj = service:CreateTrustedComponent(tostring(component_info.m_chassis_id), component_info.m_mdb_name,
        function(cur_obj)
        cur_obj.Name = component_info.m_device_name
        cur_obj.TargetComponent = component_info.m_inventory_path
        cur_obj.Type = component_info.m_type
    end)
    self.m_mdb_components[component_info.m_device_name] = obj
end

return singleton(trusted_component_mdb)